package com.example.yiparkapplication.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.location.LocationClientOption;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static com.example.yiparkapplication.constant.Constant.LOCALE_CITY_NAME;

public class Common {

    public static void failureMessage(String message, Context context){
        Looper.prepare();
        Toast.makeText(context, "连接失败，请稍后重试", Toast.LENGTH_LONG).show();
        Looper.loop();
    }

    public static JSONObject tempResponseException(String tempResponse, Context context){
        JSONObject joOut = null;
        try{
            joOut = JSON.parseObject(tempResponse);
        }catch(Exception e){
            Looper.prepare();
            Toast.makeText(context, "连接失败，请稍后再试", Toast.LENGTH_LONG).show();
            Log.e("tempResponse", tempResponse);
            Looper.loop();
        }
        if (joOut == null){
            Looper.prepare();
            Toast.makeText(context, "请求失败", Toast.LENGTH_LONG).show();
            Looper.loop();
        }
        return joOut;
    }

    public static LocationClientOption initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setNeedNewVersionRgc(true); //是否需要最新版本地址信息
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); //设置高精度迷失，可选定位模式，高精度，低功耗
        option.setCoorType("bd0911"); //默认gcj02，设置返回的定位结果坐标系
        int span = 1000;
        option.setScanSpan(span); //默认0，定位次数就是0，可以设置大于1000ms的时间间隔进行定位更新
        option.setIsNeedAddress(true); //是否需要返回地址信息
        option.setOpenGps(true); //是否使用gps
        option.setLocationNotify(true); //默认关闭，是否当GPS有效时按照1s/1的帧率输出gps结果
        option.setIsNeedLocationDescribe(true); //默认关闭，是否需要位置的语义化结果
        option.setIsNeedLocationPoiList(true); //默认关闭，是否需要POI结果
        option.setIgnoreKillProcess(false); //默认开启，定位sdk作为一个单独线程中的service，

        option.SetIgnoreCacheException(false); //默认收集，设置是否手机cache信息是否在页面stop时关闭进程，默认不关闭
        option.setEnableSimulateGps(false); //默认开启，设置是否需要过滤gps仿真结果
        return option;
    }

    public static void getLocaCity(String city){
        LOCALE_CITY_NAME = city;
    }

    public static Bitmap getHttpBitmap(String url){
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try{
            myFileUrl = new URL(url);
        }catch(MalformedURLException e){
            e.printStackTrace();
        }
        try{
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setConnectTimeout(0);
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        return bitmap;
    }

    public static int packageCode(Context context){
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try{
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        }catch(PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }
        return code;
    }

    public static String packageName(Context context){
        PackageManager manager = context.getPackageManager();
        String name = "";
        try{
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        }catch(PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }
        return name;
    }

}
